package com.example.park.park

import android.view.MenuItem
import android.widget.PopupMenu
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import androidx.core.view.GravityCompat
import androidx.lifecycle.lifecycleScope
import androidx.paging.PagingData
import androidx.paging.filter
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_base.entity.ParkBeanItem
import com.example.lib_base.mvvm.BaseActivity
import com.example.park.R
import com.example.park.databinding.ActivityParkBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = "/park/ParkActivity")
class ParkActivity : BaseActivity<ActivityParkBinding,ParkViewModel>() {
    val parkAdapter by lazy { ParkAdapter() }
    var type="类型"
    var para=""
    lateinit var datas:PagingData<ParkBeanItem>
    override fun initView() {
        binding.parkRv.apply {
            adapter=parkAdapter
            layoutManager=LinearLayoutManager(this@ParkActivity)
        }
        //点击跳转到编辑页面
        parkAdapter.setOnItemClickListener(object :OnItemClickListener{
            override fun onItemClick(item: ParkBeanItem) {
                ARouter.getInstance().build("/park/ParkEtActivity").withString("pid",item.id.toString()).navigation()
            }

        })
        //长按
        parkAdapter.setOnItemLongClickListener(object :OnItemLongClickListener{
            override fun onItemLongClick(): Boolean {
                ARouter.getInstance().build("/park/ParkVrActivity").navigation()
                return true
            }
        })
        lifecycleScope.launch {
            vm.getParkByType("标准车位",type).collect{
                datas=it
                parkAdapter.submitData(it)
            }
        }
       binding.parkFilter.setOnClickListener {
           binding.parkDrawer.openDrawer(GravityCompat.END)
       }

        binding.parkRgstatus.setOnCheckedChangeListener { radioGroup, checkedId ->
            type="状态"
            var para =""
            when(checkedId){
                R.id.rb_statuskx ->
                    para="空闲"
                R.id.rb_stauszy ->
                    para="已占用"
                else->
                    para="维修中"
            }
            binding.parkDrawer.closeDrawer(GravityCompat.END)
            lifecycleScope.launch {
                vm.getParkByType(para,type).collect{
                    parkAdapter.submitData(it)
                }
            }
        }

        binding.parkRgtype.setOnCheckedChangeListener { radioGroup, i ->
            var type=""
            when(i){
                R.id.rb_typea ->
                    type="标准车位"
                R.id.rb_typeb ->
                    type="VIP车位"
            }
            binding.parkDrawer.closeDrawer(GravityCompat.END)
            lifecycleScope.launch {
                vm.getParkByType(type,"类型").collect{
                    parkAdapter.submitData(it)
                }
            }
        }
        binding.parkSv.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                para=query!!
//                // 把查询文本转换为 Int 类型
//                val queryInt = query?.toIntOrNull()
//                if (queryInt!=null){
//                    lifecycleScope.launch {
//                        vm.getParkByType(queryInt,"指定区域").collect{
//                            parkAdapter.submitData(it)
//                        }
//                    }
//                }
                lifecycleScope.launch {
                    if (type=="区域"){
                       // val toIntOrNull = query?.toIntOrNull()
                        vm.getParkByType(query!!,type).collect{
                            parkAdapter.submitData(it)
                        }
                    }else{
                        parkAdapter.submitData(datas.filter {
                            it.licensePlate==para
                        })
                    }
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText!=null){
                    lifecycleScope.launch {
                        vm.getParkByType("标准车位","类型").collect{
                            parkAdapter.submitData(it)
                        }
                    }
                }
                return true
            }

        })
        binding.parkArea.setOnClickListener {
            binding.parkDrawer.closeDrawer(GravityCompat.END)
            type="区域"
            if (para.isNotEmpty()){
                lifecycleScope.launch {
                    vm.getParkByType(para,type).collect{
                        parkAdapter.submitData(it)
                    }
                }
            }
        }
        binding.parkCarNo.setOnClickListener {
            type=="车牌号"
            lifecycleScope.launch {
                parkAdapter.submitData(datas.filter {
                    it.licensePlate==para
                })
            }
        }
        binding.parkAdd.setOnClickListener {
            ARouter.getInstance().build("/park/ParkAddActivity").navigation()
        }
        ItemTouchHelper(object :ItemTouchHelper.Callback(){
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ): Int {
                val left = ItemTouchHelper.LEFT
                return makeMovementFlags(0,left)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.adapterPosition
                vm.delete(parkAdapter.snapshot().items.get(position).id!!)
            }

        }).attachToRecyclerView(binding.parkRv)
    }

    override fun initData() {

        binding.parkFilter.setOnClickListener {
            val popupMenu =PopupMenu(this,binding.parkFilter)
            popupMenu.inflate(R.menu.pop_menu)
            popupMenu.show()
            popupMenu.setOnMenuItemClickListener(object :PopupMenu.OnMenuItemClickListener{
                override fun onMenuItemClick(item: MenuItem?): Boolean {
                    var para =""
                    when(item?.itemId){
                        R.id.kx -> para="空闲"
                        R.id.yzy -> para="已占用"
                        R.id.wxz -> para="维修中"
                    }
                    popupMenu.dismiss()
                    lifecycleScope.launch {
                        vm.getParkByType(para,"状态").collect{
                            parkAdapter.submitData(it)
                        }
                    }
                    return true
                }
            })
        }

        vm.deleteSuccess.observe(this){
            if (it){
                lifecycleScope.launch {
                    vm.getParkByType("标准车位",type).collect{
                        datas=it
                        parkAdapter.submitData(it)
                    }
                }
            }
        }

    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch {
            vm.getParkByType("标准车位",type).collect{
                datas=it
                parkAdapter.submitData(it)
            }
        }
    }

}